package com.lepu.app.fun.hospital.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.config.UserConfig;
import com.app.utils.DateUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.eyzhs.app.R;
import com.lepu.app.fun.hospital.HospitalAdviceDetailActivity;
import com.lepu.app.fun.hospital.bean.HospitalAdvice;

public class HospitalAdviceAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	public Context mContext = null;
	private ArrayList<HospitalAdvice> mArrayList = null;

	public HospitalAdviceAdapter(Context context, ArrayList<HospitalAdvice> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.hospital_advice_item_layout, null);

			holder = new ViewHolder();

			holder.itemLayout = (LinearLayout) convertView.findViewById(R.id.layout);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.count = (TextView) convertView.findViewById(R.id.count);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final HospitalAdvice item = (HospitalAdvice) mArrayList.get(position);

        holder.title.setText(item.QuestionContent);
        holder.time.setText(DateUtil.getHMTime(item.RecordCreateTime));
        
        int count = Integer.valueOf(UserConfig.getConfigString(mContext, "NewCount_"+item.DCQID, "0"));
        if(count > 0){
        	holder.count.setText(String.valueOf(count));
        	holder.count.setVisibility(View.VISIBLE);
        }else{
        	holder.count.setVisibility(View.GONE);
        }

        holder.itemLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext, HospitalAdviceDetailActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("advice", item);
				intent.putExtras(bundle);
				((BaseFragmentActivity)mContext).startActivity(intent,true);
				
				//总数减去当前的count
				int thisCount = Integer.valueOf(UserConfig.getConfigString(mContext, "NewCount_"+item.DCQID, "0"));
				int allCount = Integer.valueOf(UserConfig.getConfigString(mContext, "NewCount", "0"));
				
				UserConfig.setConfig(mContext, "NewCount", String.valueOf(allCount - thisCount));
				
				//当前的count归零
				UserConfig.setConfig(mContext, "NewCount_"+item.DCQID, "0");
				holder.count.setVisibility(View.GONE);
			}
		});
        
		return convertView;
	}
	
	static class ViewHolder {
		LinearLayout itemLayout;
		TextView title;
		TextView time;
		TextView count;
	}

}
